package kassiopeia.agent.io.ping;

import kassiopeia.agent.AgentIDHolder;
import kassiopeia.agent.AgentStatusHolder;
import kassiopeia.agent.io.MessageSender;
import kassiopeia.commons.io.Ping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("pingSender")
public class PingSender {

    @Autowired
    private MessageSender sender;

    @Autowired
    private AgentStatusHolder statusHolder;

    @Value("${agent.jmx.url}")
    private String jmxUrl;

    private String agentId;

    public PingSender() {
        agentId = AgentIDHolder.getThisAgentId();
    }

    public void ping() {
        sender.broadcast(createPingMessage());
    }

    private Ping createPingMessage() {
        return PingFactory.getPing(agentId, jmxUrl, statusHolder.getStatus(), statusHolder.getTaskSummary());
    }
}
